package com.zlc.workflow.core.instance.controller;


import com.zlc.common.core.process.model.vo.StoneProcessLogVo;
import com.zlc.common.util.UserHelper;
import com.zlc.workflow.core.instance.cmd.NotifyNodeCmd;
import com.zlc.workflow.core.instance.model.BuildFlowParam;
import com.zlc.workflow.core.instance.model.NotifyWaitNodeParam;
import com.zlc.workflow.core.instance.model.QueryFlowDetailParam;
import com.zlc.workflow.core.instance.model.QueryNextTaskParam;
import com.zlc.workflow.core.instance.model.QueryRejectNodeParam;
import com.zlc.workflow.core.instance.model.RejectFlowParam;
import com.zlc.workflow.core.instance.model.RejectNodeVo;
import com.zlc.workflow.core.instance.model.SubmitFlowParam;
import com.zlc.workflow.core.instance.model.TerminationFlowParam;
import com.zlc.workflow.core.instance.model.WorkflowDetail;
import com.zlc.workflow.core.instance.model.WorkflowTaskNode;
import com.zlc.workflow.core.instance.service.InstanceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "流程处理相关api")
@RestController
@Slf4j
@RequestMapping("/api/process/instance")
public class InstanceController {

    @Autowired
    private InstanceService instanceService;

    /**
     * 查询流程任务详情
     */
    @ApiOperation(value = "查询流程任务详情")
    @PostMapping(value = "/workflowDetail")
    public WorkflowDetail workflowDetail(@RequestBody QueryFlowDetailParam param){
        param.setAccount(UserHelper.getNowAccount());
        return instanceService.workflowDetail(param);
    }

    /**
     * 查询流程后续任务节点
     */
    @ApiOperation(value = "查询流程后续任务节点")
    @PostMapping(value = "/nextTask")
    public WorkflowTaskNode nextTask(@RequestBody QueryNextTaskParam param){
        param.setAccount(UserHelper.getNowAccount());
        return instanceService.nextTask(param);
    }

    /**
     * 创建流程
     */
    @ApiOperation(value = "创建流程")
    @PostMapping(value = "/buildFLow")
    public WorkflowDetail buildFLow(@RequestBody BuildFlowParam param){
        param.setAccount(UserHelper.getNowAccount());
        return instanceService.buildFLow(param);
    }

    /**
     * 创建并提交流程
     */
    @ApiOperation(value = "创建并提交流程")
    @PostMapping(value = "/buildAndSubmit")
    public WorkflowDetail buildAndSubmit(@RequestBody SubmitFlowParam param){
        param.setAccount(UserHelper.getNowAccount());
        return instanceService.buildAndSubmit(param);
    }

    /**
     * 提交流程
     */
    @ApiOperation(value = "提交流程")
    @PostMapping(value = "/submit")
    public WorkflowDetail submit(@RequestBody SubmitFlowParam param){
        param.setAccount(UserHelper.getNowAccount());
        return instanceService.submit(param);
    }

    /**
     * 查询可退回节点信息
     */
    @ApiOperation(value = "查询可退回节点信息")
    @PostMapping(value = "/getRejectNodeList")
    public List<RejectNodeVo> getRejectNodeList(@RequestBody QueryRejectNodeParam param){
        param.setAccount(UserHelper.getNowAccount());
        return instanceService.getRejectNodeList(param);
    }

    /**
     * 退回流程
     */
    @ApiOperation(value = "退回流程")
    @PostMapping(value = "/reject")
    public WorkflowDetail reject(@RequestBody RejectFlowParam param){
        param.setAccount(UserHelper.getNowAccount());
        return instanceService.reject(param);
    }

    /**
     * 终止流程
     */
    @ApiOperation(value = "终止流程")
    @PostMapping(value = "/termination")
    public WorkflowDetail termination(@RequestBody TerminationFlowParam param){
        param.setAccount(UserHelper.getNowAccount());
        return instanceService.termination(param);
    }

    /**
     * 唤醒流程等待节点
     */
    @ApiOperation(value = "唤醒流程等待节点")
    @PostMapping(value = "/notifyWaitNode")
    public WorkflowDetail notifyWaitNode(@RequestBody NotifyWaitNodeParam param) {
       return instanceService.notifyWaitNode(param);
    }

    /**
     * 查询流程日志
     */
    @ApiOperation(value = "查询流程日志")
    @GetMapping(value = "/queryProcessLog")
    public List<StoneProcessLogVo> queryProcessLog(@RequestParam(value = "processInstanceId")String processInstanceId) {
        return instanceService.queryProcessLog(processInstanceId);
    }
}
